unit uMainFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,

  GlScene, GlObjects, GlCadencer, GLWin32Viewer, GLRenderContextInfo, GlWindowsFont, GlHudObjects,
  GlMaterial,
  VectorGeometry,
  uBaseButton,
  uTweenButtons,

  uTweener;
type
  TfrmMain = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    {Main Objects}
    fGlScene: TGlScene;
    fCadencer: TGLCadencer;
    fViewer: TGLSceneViewer;
    fCamera: TGlCamera;
    fMainDummy: TGlDummyCube;
    fFpsTimer: Single;
    fMatLib: TGlMaterialLibrary;

    {Text Objects}
    fWinFont: TGLWindowsBitmapFont;
    fHudText: TGLHUDText;
    fBack: TGlHudSprite;

    {SimpleButton}
    fAchievementsBtn: TGUIAlphaButton;
    fPlayBtn: TGUIAlphaButton;
    fDownloadBtn: TGUISliderButton;
    f3d_GreenBtn: TGUI3dSlideButton;
    fPriceBtn: TGUIScaleButton;

    {Tweener}
    fTweener: TTweener;
  public
    Procedure InitFontAndSprites;
    Procedure InitMaterials;
    Procedure InitTweener;
    Procedure OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
  end;

var
  frmMain: TfrmMain;

implementation
uses
  glKeyboard, GLCrossPlatform,
  uDemoInit;
{$R *.dfm}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  fFpsTimer := 0;
  CreateDemo(self, fGlScene, fCadencer, fViewer);
  
  fCamera := TGlCamera.CreateAsChild(fGlScene.Objects);
  with fCamera do
  begin
    Position.SetPoint(0, 0, 5);
    Direction.SetVector(0, 0, -1);
    Up.SetVector(0, 1, 0);
    CameraStyle := csOrthogonal;
    SceneScale := 0.1;
  end;

  fMainDummy := TGLDummyCube.CreateAsChild(fGlScene.Objects);
  fMainDummy.ObjectsSorting := osNone;

  fViewer.Camera := fCamera;
  fCadencer.OnProgress := OnCadencerProgress;

  fMatLib := TGlMaterialLibrary.Create(self);

  InitMaterials;
  InitTweener;
  InitFontAndSprites;
end;

procedure TfrmMain.FormActivate(Sender: TObject);
begin
  fCadencer.Enabled := true;
end;

Procedure TfrmMain.InitFontAndSprites;
  Function AddSprite(aMatName: String; aX, aY: integer; aW, aH: Integer): TGlHudSprite;
  begin
    result := TGlHudSprite.CreateAsChild(fMainDummy);
    with result do
    begin
      Material.LibMaterialName := aMatName;
      Material.MaterialLibrary := fMatLib;
      Width  := aW;
      Height := aH;
      Position.SetPoint(aX, aY, 0);
    end;
  end;

begin
  fWinFont := TGLWindowsBitmapFont.Create(self);
  fWinFont.Font.Name := 'Arial';

  fBack := AddSprite('back_1', 200, 150, 400, 300);

  fHudText := TGLHUDText.CreateAsChild(fMainDummy);
  with fHudText do
  begin
    BitmapFont := fWinFont;
    Text := 'Lampogolovii presents ''Buttons Demo''';
    Position.SetPoint(0, 5, 0);
    ModulateColor.SetColor(50/255, 105/255, 140/255, 1);
    Alignment := taRightJustify;
  end;
  fHudText.Rotation := 90;

  fAchievementsBtn := TGUIAlphaButton.CreateAsChild(fMainDummy);
  fAchievementsBtn.SetMaterials('btn_1', 'btn_2', fMatLib);
  fAchievementsBtn.Position.SetPoint(200, 60, 0);
  fAchievementsBtn.Show;

  fPlayBtn := TGUIAlphaButton.CreateAsChild(fMainDummy);
  fPlayBtn.SetMaterials('btn_3', 'btn_4', fMatLib);
  fPlayBtn.Position.SetPoint(200, 115, 0);
  fPlayBtn.Show;

  fDownloadBtn := TGUISliderButton.CreateAsChild(fMainDummy);
  fDownloadBtn.SetMaterials('download_1', 'download_ext', fMatLib);
  fDownloadBtn.Position.SetPoint(90, 270, 0);
  fDownloadBtn.Tweener := fTweener;
  fDownloadBtn.Show;

  f3d_GreenBtn := TGUI3dSlideButton.CreateAsChild(fMainDummy);
  f3d_GreenBtn.SetMaterials('3d_1', '3d_2', fMatLib);
  f3d_GreenBtn.Position.SetPoint(200, 200, 0);
  f3d_GreenBtn.Tweener := fTweener;
  f3d_GreenBtn.Show;

  fPriceBtn := TGUIScaleButton.CreateAsChild(fMainDummy);
  fPriceBtn.Tweener := fTweener;
  fPriceBtn.SetMaterial('price_btn', fMatLib);
  fPriceBtn.Position.SetPoint(330, 230, 0);
  fPriceBtn.Show;
end;

Procedure TfrmMain.InitMaterials;
begin
  AddMaterials(fMatLib, str_ImagesFolder + '\ButtonsDemo\',
               ['back_1', 'download_1', 'download_ext', '3d_1', '3d_2', 'price_btn'],
               ['back_1', 'download_1', 'download_ext', '3d_1', '3d_2', 'price_btn']);

  AddMaterials(fMatLib, str_ImagesFolder + '\ButtonsDemo\',
               ['achievements_1', 'achievements_2', 'play_1', 'play_2'],
               ['btn_1', 'btn_2', 'btn_3', 'btn_4']);
end;

Procedure TfrmMain.InitTweener;
begin
  fTweener := TTweener.Create;
end;

Procedure TfrmMain.OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
var
  MPos: TPoint;
begin
  fTweener.Update(deltaTime);

  GetCursorPos(Mpos);
  MPos := fViewer.ScreenToClient(MPos);

  fPlayBtn.SetMouseState(MPos.x, MPos.y, IsKeyDown(vk_LButton));
  fAchievementsBtn.SetMouseState(MPos.x, MPos.y, IsKeyDown(vk_LButton));
  fDownloadBtn.SetMouseState(MPos.x, MPos.y, IsKeyDown(vk_LButton));
  f3d_GreenBtn.SetMouseState(MPos.x, MPos.y, IsKeyDown(vk_LButton));
  fPriceBtn.SetMouseState(MPos.x, MPos.y, IsKeyDown(vk_LButton));

  fViewer.Invalidate;
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  FreeAndNil(fTweener);
end;

end.
